package com.gk.springcloudauth2email.config.jwt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * @Author poison
 * @Date 2020/10/14 11:26
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfigJwt extends ResourceServerConfigurerAdapter {

    // 配置资源服务的id
    public static String RESOURCE_ID = "email";

    @Autowired
    private TokenStore tokenStore;

    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID)//资源 id
                .tokenStore(tokenStore)
                .stateless(true);
    }
    /**
     * 安全访问策略
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                // 令牌访问资源的范围
                .antMatchers("/**").access("#oauth2.hasScope('all')")
                .anyRequest().permitAll()
                //.anyRequest().authenticated()
                .and().csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

}
